const app = getApp();
const {
  searchPrint,
  editPrint
} = require('../../../config.js')

Page({
  data: {
    codes: '304a2650d8b5', 
    disabled: true,
    btnTip: false,
    setTimeoutTip: true,
    border: false,
    focus: false,
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // T1001011901000026
  },
  // 上线前删除
  codesChange(e){
    console.log(e.detail)
    this.setData({
      codes: e.detail
    })
  },
  /**
 * 生命周期函数--监听页面隐藏
 */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.hideLoading()
    this.data.setTimeoutTip = false
  },

  // 搜索设备
  searchPrinter(e){  
    // app.globalData.getdata.id = 29
    if (this.data.setTimeoutTip) {
      wx.showLoading({
        title: '读取中...',
      })
      this.setData({ btnTip: true })

      app.appRequest({
        url: searchPrint,
        data: {
          id: app.globalData.getdata.id ? app.globalData.getdata.id : '',
          boxCode: this.data.codes
        },
        success: (res) => {
          app.globalData.getdata.id = res.data.bean.id
          if (res.data.err) {
            wx.hideLoading()
            wx.showToast({
              title: '读取超时',
              icon:'none'
            })
            app.globalData.getdata.id = ''
            this.setData({ btnTip: false })
          } else {
            if (res.data.bean.printerType) {
              wx.hideLoading()
              wx.showToast({
                title: '读取成功',
              })
              let data = res.data.bean
              if (data.paperType) {
                data.paperType = data.paperType.toString()
              }
              if (data.colourType) {
                data.colourType = data.colourType.toString()
              }
              
              this.setData({ data, btnTip: false })
            } else {
              setTimeout(() => { this.searchPrinter() }, 2000)
            }
          }
          
          // console.log(app.globalData.getdata)
        }
      })
    }
  },
  changeradioA3(e){
    let data = this.data.data;
    data.paperType = e.detail
    this.setData({
      data: data,
      changetype: true
    })
  },
  changeradioColor(e){
    let data = this.data.data;
    data.colourType = e.detail
    this.setData({
      data: data,
      changetype: true
    })
  },
  // 纠错打开
  showChangeType(){
    this.setData({
      disabled: false,
      border: true,
      focus: true,
    })
  },
  typeChange(e){
    console.log(e.detail.value)
    this.setData({
      disabled: true,
      printerType: e.detail.value,
      changetype: true
    })
    console.log(this.data.printerType)
  },
  scanCode: function () {
    let that = this;
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        if (res.scanType != 'CODE_128') {
          wx.showToast({
            title: '请扫描盒子上的条形码',
            icon: 'none',
            duration: 2000
          })
          return
        } else {
          if (res.result.substring(0, 6) != '100101' && res.result.substring(0, 7) != 'T100101') {
            wx.showToast({
              title: '请扫描盒子上的条形码',
              icon: 'none',
              duration: 2000
            })
            return
          }
          that.setData({
            codes: res.result,
          })
        }

      }
    })
  },
  beginRead(){
    if (!this.data.data){
      wx.showToast({
        title: '请先扫码搜索设备',
        icon: 'none'
      })
    }else{
      if (!this.data.data.paperType || !this.data.data.colourType){
        wx.showToast({
          title: '请选择是否支持A3和支持彩色',
          icon: 'none'
        })
      }else{
        if (this.data.changetype){
          // 修改打印机信息
          app.appRequest({
            url: editPrint,
            data: {
              id: app.globalData.getdata.id,
              printerType: this.data.printerType || this.data.data.printerType,
              paperType: this.data.data.paperType,
              colourType: this.data.data.colourType
            },
            success: (res) => {
              wx.hideNavigationBarLoading();
              if (res.code == 0) {
                wx.navigateTo({
                  url: '/pages/gatherData/getData2/getData2',
                })
              }else{
                wx.showToast({
                  title: res.msg
                })
              }
            }
          })
        }else{
          wx.navigateTo({
            url: '/pages/gatherData/getData2/getData2',
          })
        }

        app.globalData.getdata.A3 = this.data.data.paperType == "2" ? true : false;
        console.log(255, app.globalData.getdata)
      }
    }
  }
})